package com.example.testapplication.ui.login;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnimationUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.constant.Constant;
import com.example.testapplication.databinding.FragLoginBinding;
import com.example.testapplication.event.Event;
import com.example.testapplication.manager.InputTextManager;
import com.example.testapplication.other.KeyboardWatcher;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LoginFragment extends BaseFragment<FragLoginBinding,LoginFragmentViewModel> implements KeyboardWatcher.SoftKeyboardStateListener {

    /** logo 缩放比例 */
    private final float mLogoScale = 0.8f;
    /** 动画时间 */
    private final int mAnimTime = 300;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_login;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        postDelayed(() -> {
            KeyboardWatcher.with(getActivity())
                    .setListener(LoginFragment.this);
        }, 500);
        InputTextManager.with(getActivity())
                .addView(binding.etLoginPhone)
                .addView(binding.etLoginPassword)
                .setMain(binding.btnLoginCommit)
                .build();

    }

    @Override
    public void initViewObservable() {
        viewModel.uc.pClickEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer){
                    case 1:
                        NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.regist);
                        break;
                    case 2:
                        NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.forget);
                        break;
                    case 3:
                        hideKeyboard(getActivity().getCurrentFocus());
                        if(viewModel.userName.get().length()!=11){
                            binding.etLoginPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.btnLoginCommit.showError(3000);
                            ToastUtils.showShort(R.string.common_phone_input_error);
                            return;
                        }
//                        hideKeyboard(getActivity().getCurrentFocus());
                        if (true) {
                            binding.btnLoginCommit.showProgress();
                            postDelayed(() -> {
                                binding.btnLoginCommit.showSucceed();
                                postDelayed(() -> {
                                    RxBus.getDefault().post(new Event(Constant.EVENT_FINISH));
                                }, 1000);
                            }, 2000);
                            return;
                        }
                        break;

                }

            }
        });

    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {
        // 执行位移动画
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(binding.llLoginBody, "translationY", 0, -binding.btnLoginCommit.getHeight());
        objectAnimator.setDuration(mAnimTime);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();

        // 执行缩小动画
        binding.ivLoginLogo.setPivotX(binding.ivLoginLogo.getWidth() / 2f);
        binding.ivLoginLogo.setPivotY(binding.ivLoginLogo.getHeight());
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(binding.ivLoginLogo, "scaleX", 1f, mLogoScale);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(binding.ivLoginLogo, "scaleY", 1f, mLogoScale);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(binding.ivLoginLogo, "translationY", 0f, -binding.btnLoginCommit.getHeight());
        animatorSet.play(translationY).with(scaleX).with(scaleY);
        animatorSet.setDuration(mAnimTime);
        animatorSet.start();
    }

    @Override
    public void onSoftKeyboardClosed() {
        // 执行位移动画
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(binding.llLoginBody, "translationY", binding.llLoginBody.getTranslationY(), 0f);
        objectAnimator.setDuration(mAnimTime);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();

        if (binding.ivLoginLogo.getTranslationY() == 0) {
            return;
        }

        // 执行放大动画
        binding.ivLoginLogo.setPivotX(binding.ivLoginLogo.getWidth() / 2f);
        binding.ivLoginLogo.setPivotY(binding.ivLoginLogo.getHeight());
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(binding.ivLoginLogo, "scaleX", mLogoScale, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(binding.ivLoginLogo, "scaleY", mLogoScale, 1f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(binding.ivLoginLogo, "translationY", binding.ivLoginLogo.getTranslationY(), 0f);
        animatorSet.play(translationY).with(scaleX).with(scaleY);
        animatorSet.setDuration(mAnimTime);
        animatorSet.start();
    }
}
